//Documentation complete
/**
 * @author Alex
 * @version 2009FE02
 */

package ui.internal;

import problemDomain.*;
import control.*;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Servlet to handle patron login validation. 
 */
public class ValidateLogin extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = 7848053399350094114L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */

	@Override
	protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String passcode = request.getParameter("passcode");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		if (passcode != null) {
			try {
				EmployeeAccountControl accCtrl = new EmployeeAccountControl();
				EmployeeAccountBean eab = accCtrl.validate(passcode);
				HttpSession session = request.getSession();
				session.setAttribute("employee", eab);
				response.sendRedirect("switchboard");
			} catch (ErrorBean e) {
				response.sendRedirect("login?message=" + e.getMessage());
			}
		} else if (username != null && password != null) {
			try {
				EmployeeAccountControl accCtrl = new EmployeeAccountControl();
				EmployeeAccountBean eab = accCtrl.validate(username, password);
				HttpSession session = request.getSession();
				session.setAttribute("employee", eab);
				response.sendRedirect("switchboard");
			} catch (ErrorBean e) {
				response.sendRedirect("login?message=" + e.getMessage());
			}
		} else {
			response.sendRedirect("login.jsp?message=");
		}

	}

	/* END (HttpServlet) Methods */

}
